#include "nm_mem.h"


klookaside::klookaside( ULONG Size, ULONG Tag /*= NM_LOOKASIDE_TAG*/, POOL_TYPE PoolType /*= PagedPool*/ )
	: m_PagedLookasideList(NULL), m_NPagedLookasideList(NULL), m_PoolType(PoolType);
{
	if (PoolType == NonPagedPool) {

		m_NPagedLookasideList = new(NonPagedPool, Tag) NPAGED_LOOKASIDE_LIST;
		ExInitializeNPagedLookasideList(m_NPagedLookasideList, 
			NULL, 
			NULL, 
			0, 
			Size, 
			Tag, 
			0);
	}
	else {

		m_PagedLookasideList = new(NonPagedPool, Tag) PAGED_LOOKASIDE_LIST;
		ExInitializePagedLookasideList(m_PagedLookasideList, 
			NULL, 
			NULL, 
			0, 
			Size, 
			Tag, 
			0);
	}
}

void klookaside::Release()
{
	if (m_PagedLookasideList != NULL) {

		ExDeletePagedLookasideList(m_PagedLookasideList);
		delete m_PagedLookasideList;
		m_PagedLookasideList = NULL;
	}

	if (m_NPagedLookasideList != NULL) {

		ExDeleteNPagedLookasideList(m_NPagedLookasideList);
		delete m_NPagedLookasideList;
		m_NPagedLookasideList = NULL;
	}
}

PVOID klookaside::Allocate()
{
	PVOID Buffer = NULL;

	if (m_PoolType == PagedPool) {

		Buffer = ExAllocateFromPagedLookasideList(m_PagedLookasideList);
	}
	else if (m_PoolType == NonPagedPool) {

		Buffer = ExAllocateFromNPagedLookasideList(m_NPagedLookasideList);
	}
	
	return Buffer;
}


VOID klookaside::Free( PVOID Buffer )
{
	if (Buffer == NULL) {

		return;
	}

	if (m_PoolType == PagedPool) {

		ExFreeToPagedLookasideList(m_PagedLookasideList, Buffer);
	}
	else if (m_PoolType == NonPagedPool) {

		ExFreeToNPagedLookasideList(m_NPagedLookasideList, Buffer);
	}
}
